<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
      <!-- 用了一个行列布局 -->
        <treeTools :tree-node="company" :isRoot="true" @addDepts="addDepts"></treeTools>
      <el-tree :data="departs" :props="defaultProps" default-expand-all>
        <treeTools slot-scope="{ data }" :tree-node="data" @delDepts="getDepartments()" @addDepts="addDepts" @editDepts="editDepts"></treeTools>
      </el-tree>
      </el-card>
      <AddDept ref="addDept" :showDialog.sync="showDialog" :treeNode="node" @addDepts="getDepartments"></AddDept>
    </div>
  </div>
</template>

<script>
import treeTools from '@/views/departments/components/tree-tools'
import AddDept from '@/views/departments/components/add-dept'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
export default {
  components: {
    treeTools,
    AddDept
  },
  data() {
    return {
      departs: [{ name: '总裁办',manager: '刘德华', children: [{ name: '董事会', manager: '张学友' }] },
        { name: '行政部', manager: '郭富城' }, { name: '人事部', manager: '黎明' }],
      defaultProps: {
        label: 'name', // 表示 从这个属性显示内容
        children: 'children'
      },
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人' },
      showDialog: false,
      node: null // 当前点击的部门项
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const data = await getDepartments()
      this.company = { name: data.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(data.depts, '')
    },
    addDepts(node) {
      this.showDialog = true
      this.node = node
    },
    editDepts(node) {
      this.showDialog = true
      this.node = node
      // 调用子组件方法
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}

</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
